<?php
/**
 * @ClassName: Agent
 * @Description: 代理信息
 * @author: wyaohu
 * @Date: 2024/9/8 21:36
 * @Version: V1.0.0
 */

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;
use tpext\areacity\api\model\Areacity;

class Agent extends Model
{
    use SoftDelete;

    protected $name = 'agent';

    protected $autoWriteTimestamp = 'dateTime';

    protected $append = ['pcat', 'total_amount', 'balance', 'attract_name', 'operation_name'];

    public static $agent_type = [
        1 => '省代理',
        2 => '市代理',
        3 => '区代理',
    ];

    public function getPcatAttr($value, $data)
    {
        $agent_region = '';
        $province = '';
        $city = '';
        $area = '';
        if ($data['agent_type'] == 1) {
            //省代理
            if (isset($data['province']) && !empty($data['province'])) {
                $province = Areacity::where('id', '=', $data['province'])->find();
                if (!empty($province)) {
                    $province = $province['ext_name'];
                }
            }
            $agent_region = $province;
        } elseif ($data['agent_type'] == 2) {
            //市代理
            if (isset($data['province']) && !empty($data['province'])) {
                $province = Areacity::where('id', '=', $data['province'])->find();
                if (!empty($province)) {
                    $province = $province['ext_name'];
                }
            }
            if (isset($data['city']) && !empty($data['city'])) {
                $city = Areacity::where('id', '=', $data['city'])->find();
                if (!empty($city)) {
                    $city = $city['ext_name'];
                }
            }
            $agent_region = $province . $city;
        } else {
            //区县代理
            if (isset($data['province']) && !empty($data['province'])) {
                $province = Areacity::where('id', '=', $data['province'])->find();
                if (!empty($province)) {
                    $province = $province['ext_name'];
                }
            }
            if (isset($data['city']) && !empty($data['city'])) {
                $city = Areacity::where('id', '=', $data['city'])->find();
                if (!empty($city)) {
                    $city = $city['ext_name'];
                }
            }
            if (isset($data['area']) && !empty($data['area'])) {
                $area = Areacity::where('id', '=', $data['area'])->find();
                if (!empty($area)) {
                    $area = $area['ext_name'];
                }
            }
            $agent_region = $province . $city . $area;
        }

        return $agent_region;
    }

    public function getTotalAmountAttr($value, $data)
    {
        return 0;
    }

    public function getBalanceAttr($value, $data)
    {
        return 0;
    }

    public function getAttractNameAttr($value, $data)
    {
        $res = AgentAward::where('type', 1)->where('level', $data['attract_level'])->find();
        if ($res) {
            return $res['name'];
        }

        return 'Z0';
    }

    public function getOperationNameAttr($value, $data)
    {
        $res = AgentAward::where('type', 3)->where('level', $data['operation_level'])->find();
        if ($res) {
            return $res['name'];
        }

        return '无';
    }

    /**
     * 说明: 定义反向关联, 代理关联至用户表
     * @return \think\model\relation\BelongsTo
     */
    public function member()
    {
        return $this->belongsTo(Member::class, 'member_id', 'id');
    }

}